
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Wei Chen <acee06.weichen@gmail.com>
 */
public class TaskThreadDemo {

    public static void main(String[] args) {
        try {
            // Create tasks
            Runnable printA = new PrintChar('a', 100);
            Runnable printB = new PrintChar('b', 100);
            Runnable printC = new PrintChar('c', 100);
            //Runnable print100 = new PrintNum(100);

            // Create threads
            Thread thread1 = new Thread(printA);
            Thread thread2 = new Thread(printB);
            Thread thread3 = new Thread(printC);
            
            thread1.setPriority(Thread.MAX_PRIORITY);
            thread3.setPriority(Thread.MIN_PRIORITY);

            // Start threads
            thread1.start();
            thread2.start();
            thread3.start();
            
            thread1.join();
            thread2.join();
            thread3.join();
            
            System.out.println("All threads finish!");
        }
        catch (InterruptedException ex) {
            Logger.getLogger(TaskThreadDemo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

// The task for printing a specified character in specified times
class PrintChar implements Runnable {

    private char charToPrint; // The character to print
    private int times; // The times to repeat

    /** Construct a task with specified character and number of
     *  times to print the character
     */
    public PrintChar(char c, int t) {
        charToPrint = c;
        times = t;
    }

    /** Override the run() method to tell the system
     *  what the task to perform
     */
    public void run() {
        for (int i = 0; i < times; i++) {
            System.out.print(charToPrint);
        }
    }
}

// The task class for printing number from 1 to n for a given n
class PrintNum implements Runnable {

    private int lastNum;

    /** Construct a task for printing 1, 2, ... i */
    public PrintNum(int n) {
        lastNum = n;
    }

    /** Tell the thread how to run */
    public void run() {
        for (int i = 1; i <= lastNum; i++) {
            System.out.print(" " + i);
        }
    }
}